﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace hamster
{
    class WasteGoomba : WorldObject
    {
        public const float WASTE_SLOWDOWN = .5f;
        public const float EVOLVE_TIME = 10;
        public const float ATTACK_AMOUNT = .2f;
        public const float DIGESTION_TIME = 1;
        public const float GOOMBA_SPEED = .2f;
        public const int ENEMY_KILL_SCORE = 10;

        bool hasLeftPlayer_;
        //bool onGround_;
        float evolveTime_;
        public bool activeEnemy_;
        public float digestionTime_;
        bool permanentCrap_;
        public float direction_;
        float offset = Game1.RandomFloat;

        //Sound
        SoundEffect se_GWasteSpeak;
        SoundEffect se_crunch;

        public WasteGoomba(Vector2 position, float rotation, ObjectPlacer parent) : base(position, rotation, parent)
        {
            debugColor_ = Color.Blue;
            objectScale_ = Player.PLAYER_SIZE / 1;
            hasLeftPlayer_ = false;
            evolveTime_ = EVOLVE_TIME + Game1.RandomNegPos() * EVOLVE_TIME / 2;
            activeEnemy_ = false;
            digestionTime_ = DIGESTION_TIME + (float)Game1.random.NextDouble() * DIGESTION_TIME;
            direction_ = GOOMBA_SPEED + Game1.RandomNegPos() * GOOMBA_SPEED / 2;
            if (Game1._player.velocity.X >= 0)
                direction_ *= -1;
            //onGround_ = false;

            //Sound
            se_GWasteSpeak = Game1._player.g.Content.Load<SoundEffect>("Sounds\\GWasteSpeak");
            se_crunch = Game1._player.g.Content.Load<SoundEffect>("Sounds\\crunch");
            se_crunch.Play();
        }

        public override bool Update(float dt, Player player)
        {
            base.Update(dt, player);
            Vector2 playerPos = new Vector2(player.position.X, player.position.Y);
            // If active and intercepting
            if (activeEnemy_)
            {
                if (boundingBox_.Intersects(player.boundingBox))
                {
                    // If player has left its location yet
                    if (hasLeftPlayer_)
                    {
                        Vector2 playerDir = Vector2.Subtract(playerPos, position_);
                        playerDir = Vector2.Transform(playerDir, Matrix.CreateRotationZ(-rotation_));

                        if (playerDir.Y > 0 && Math.Abs(playerDir.Y) > Math.Abs(playerDir.X))
                        {
                            // Above collision
                            player.score += ENEMY_KILL_SCORE;
                            player.velocity.Y = Player.JUMP_SPEED;
                        }
                        else
                        {
                            // Slow the player's velocity
                            hasLeftPlayer_ = false;
                            // Attack brightness
                            player.brightness -= ATTACK_AMOUNT;
                            if (player.velocity.X < 0)
                                player.velocity.X = Player.SPEED_CAP;
                            else
                                player.velocity.X = -Player.SPEED_CAP;
                            //player.velocity.X = MathHelper.Clamp(player.velocity.X, -WASTE_SLOWDOWN, WASTE_SLOWDOWN);
                            //player.velocity.Y = MathHelper.Clamp(player.velocity.Y, -WASTE_SLOWDOWN, WASTE_SLOWDOWN);
                            //TODO Absorb extra energy

                            for(int i = 0; i < 50; i++)
                                Particles.instance.Add(player.position, (Game1.RandomVector - Vector3.One * 0.5f) * 2f, Game1.RandomFloat + 0.25f, Game1.RandomFloat + .25f, 0f);

                        }
                        return true;
                    }
                }
                else if (Vector2.Distance(playerPos, position_) > 1)
                {
                    hasLeftPlayer_ = true;
                }

                // Goomba movement
                //if (position_.Length() < Wheel.RADIUS_IN * 1.025f)
                //{
                //    position_ = Vector2.Subtract(position_, new Vector2(upDir.X * dt, upDir.Y * dt));
                //}
                //if (position_.Length() > Wheel.RADIUS_IN * 1.025f)
                //{
                //    position_ = Vector2.Add(position_, new Vector2(upDir.X * dt, upDir.Y * dt));
                //}
                //if (position_.Length() >= Wheel.RADIUS_IN * 1.025f)
                //    onGround_ = true;

                float localRotation = direction_ * dt;
                position_ = Vector2.Transform(position_, Matrix.CreateRotationZ(localRotation));
                rotation_ += localRotation;
            }
            else
            {
                digestionTime_ -= dt;
                if (digestionTime_ < 0)
                {
                    activeEnemy_ = true;
                    position_ = new Vector2(player.position.X, player.position.Y);
                    rotation_ = player.rotation;
                    se_GWasteSpeak.Play();
                }
            }
            return false;
        }

        public override void Draw(GraphicsDevice device)
        {
            if (activeEnemy_)
            {

                ObjectPlacer._enemyEffect.Parameters["World"].SetValue(Matrix.CreateRotationZ((float) Math.PI + offset * (float) Math.PI) * World);
                ObjectPlacer._enemyEffect.Parameters["Time"].SetValue(Game1.time_duration);
                ObjectPlacer._enemyEffect.CommitChanges();
                device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, ObjectPlacer._quad, 0, ObjectPlacer._quad.Length / 3);

            }
            //                base.Draw(device);
        }
        public void DrawBallon(GraphicsDevice device)
        {
            if (activeEnemy_)
            {

                ObjectPlacer._enemyEffect.Parameters["World"].SetValue(Matrix.CreateTranslation(0, -.5f, 0) * Matrix.CreateScale(1, 2f, 1f) * Matrix.CreateRotationZ(3.145f + 0.2f * Game1.Sin(Game1.time_duration)) * World);
                ObjectPlacer._enemyEffect.CommitChanges();
                device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, ObjectPlacer._quad, 0, ObjectPlacer._quad.Length / 3);

            }
            //                base.Draw(device);
        }

        //float offset = Game1.RandomFloat;
        public void DrawGlow(GraphicsDevice device)
        {
            if (activeEnemy_)
            {
                ObjectPlacer._enemyEffect.Parameters["World"].SetValue(Matrix.CreateTranslation(0, -.5f, 0)  * Matrix.CreateRotationZ(3.145f + 0.2f * Game1.Sin(Game1.time_duration)) * World);
                ObjectPlacer._enemyEffect.Parameters["glow"].SetValue( offset + Game1.time_duration);
                ObjectPlacer._enemyEffect.CommitChanges();
                device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, ObjectPlacer._quad, 0, ObjectPlacer._quad.Length / 3);

            }
            //                base.Draw(device);
        }
    }
}
